package com.bitao.system.service.question.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.bitao.comstants.Constants;
import com.bitao.system.domain.question.dto.QuestionAddDTO;
import com.bitao.system.domain.question.dto.QuestionEditDTO;
import com.bitao.system.domain.question.dto.QuestionQueryDTO;
import com.bitao.system.domain.question.entity.Question;
import com.bitao.system.domain.question.es.QuestionES;
import com.bitao.system.domain.question.vo.QuestionDataVO;
import com.bitao.system.domain.question.vo.QuestionVO;
import com.bitao.system.elasticsearch.QuestionRepository;
import com.bitao.enums.ResultCode;
import com.bitao.exception.ServiceException;
import com.bitao.system.manage.QuestionCacheManager;
import com.bitao.system.mapper.question.QuestionMapper;
import com.bitao.system.service.question.IQuestionService;
import com.github.pagehelper.PageHelper;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Arrays;
import java.util.List;
import java.util.Set;
import java.util.stream.Collectors;

/**
 * Created with IntelliJ IDEA.
 * Description:
 * Author:  LiuTao
 * Version: 1.0
 * Date:    2025-05-27  17:50
 */
@Service
public class QuestionServiceImpl implements IQuestionService {

    @Autowired
    private QuestionMapper questionMapper;

    @Autowired
    private QuestionRepository questionRepository;

    @Autowired
    private QuestionCacheManager questionCacheManager;

    @Override
    public List<QuestionVO> list(QuestionQueryDTO questionQueryDTO) {
        String excludeStr = questionQueryDTO.getExcludeStr();
        if (StrUtil.isNotBlank(excludeStr)) {
            String[] excludeIdArr = excludeStr.split(Constants.SPLIT_CHAR);
            Set<Long> excludeIdSet = Arrays.stream(excludeIdArr)
                    .map(Long::valueOf)
                    .collect(Collectors.toSet());
            questionQueryDTO.setExcludeIdSet(excludeIdSet);
        }
        PageHelper.startPage(questionQueryDTO.getPageNum(), questionQueryDTO.getPageSize());
        return questionMapper.selectQuestionList(questionQueryDTO);
    }

    @Override
    public boolean add(QuestionAddDTO questionAddDTO) {
        List<Question> questionList = questionMapper.selectList(new LambdaQueryWrapper<Question>()
                .eq(Question::getTitle, questionAddDTO.getTitle()));
        if (CollectionUtil.isNotEmpty(questionList)) {
            throw new ServiceException(ResultCode.FAILED_ALREADY_EXISTS);
        }
        Question question = new Question();
        BeanUtils.copyProperties(questionAddDTO, question);
        int insert = questionMapper.insert(question);
        if (insert <= 0) {
            return false;
        }
        QuestionES questionES = BeanUtil.copyProperties(questionAddDTO, QuestionES.class);
        questionRepository.save(questionES);
        questionCacheManager.addCache(questionES.getQuestionId());
        return true;
    }

    @Override
    public QuestionDataVO detail(Long questionId) {
        Question question = getQuestion(questionId);
        QuestionDataVO questionDataVO = new QuestionDataVO();
       BeanUtils.copyProperties(question, questionDataVO);
       return questionDataVO;
    }



    @Override
    public int edit(QuestionEditDTO questionEditDTO) {
        Question oldQuestion = getQuestion(questionEditDTO.getQuestionId());

        oldQuestion.setTitle(questionEditDTO.getTitle());
        oldQuestion.setDifficulty(questionEditDTO.getDifficulty());
        oldQuestion.setTimeLimit(questionEditDTO.getTimeLimit());
        oldQuestion.setSpaceLimit(questionEditDTO.getSpaceLimit());
        oldQuestion.setContent(questionEditDTO.getContent());
        oldQuestion.setQuestionCase(questionEditDTO.getQuestionCase());
        oldQuestion.setDefaultCode(questionEditDTO.getDefaultCode());
        oldQuestion.setMainFuc(questionEditDTO.getMainFuc());

        QuestionES questionES = BeanUtil.copyProperties(oldQuestion,QuestionES.class);
        questionRepository.save(questionES);
        return questionMapper.updateById(oldQuestion);
    }

    @Override
    public int delete(Long questionId) {
        getQuestion(questionId);
        questionRepository.deleteById(questionId);
        questionCacheManager.deleteCache(questionId);
        return questionMapper.deleteById(questionId);
    }

    /**
     * 通过questionId获取question信息
     * @param questionId
     * @return
     */
    private Question getQuestion(Long questionId) {
        Question question = questionMapper.selectById(questionId);
        if (question == null) {
            throw new ServiceException(ResultCode.FAILED_ALREADY_EXISTS);
        }
        return question;
    }
}
